/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.action;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import ca.usherbrooke.agent.SimpleMoveAgentController;
import ca.usherbrooke.application.astar.action.AStarMoveAction;
import ca.usherbrooke.application.astar.location.BasicLocation;
import ca.usherbrooke.application.astar.location.CompleteLocation;
import ca.usherbrooke.tools.position.Vector2D;

/**
 * @author Cody Stoutenburg
 * 
 */
public abstract class Action implements Serializable {
	private static final long serialVersionUID = 1L;

	public static List<Action> generateAllAction(
			SimpleMoveAgentController agent, List<Float> values, Long duration) {
		List<Action> allActions = new ArrayList<Action>();
		for (int i = 0; i < values.size(); ++i) {
			MoveAction move = new MoveAction(duration, i);
			if (move.init(agent, values)) {
				allActions.add(move);
			}

			DropObjectAction drop = new DropObjectAction(duration, i);
			if (drop.init(agent, values)) {
				allActions.add(drop);
			}

			GrabObjectAction grab = new GrabObjectAction(duration, i);
			if (grab.init(agent, values)) {
				allActions.add(grab);
			}
		}

		return allActions;
	}

	public static List<Action> generateAllActionWithExplorationIfNoOther(
			SimpleMoveAgentController agent, List<Float> values, Long duration) {
		List<Action> allActions = generateAllAction(agent, values, duration);

		if (allActions.isEmpty()) {
			allActions = ExplorationAction.generateExplorationAction(agent,
					duration);
		}

		return allActions;
	}

	private Long begin;
	private final Long duration;
	protected SimpleMoveAgentController agentController;

	public Action(Long duration) {
		super();
		this.begin = 0L;
		this.duration = duration;
	}

	public final Boolean init(SimpleMoveAgentController controller,
			List<Float> vals) {
		agentController = controller;
		return init(vals) && canDoAction();
	}

	/**
	 * try to init the action and return false if it cannot
	 * 
	 * @param vals
	 * @return
	 */
	protected abstract Boolean init(List<Float> vals);

	protected abstract Boolean canDoAction();

	public void applyAction() {
		begin = this.agentController.getWorld().getRealTimeElapse();
		apply();
	}

	protected abstract void apply();

	public Boolean actionIsFinish() {
		Long currentTime = this.agentController.getWorld().getRealTimeElapse();
		if (currentTime > begin + duration) {
			return true;
		}
		return isFinish();
	}

	protected abstract Boolean isFinish();

	public abstract Float generateEstimatedCost();

	public abstract Float generateEstimatedWon();

	public Boolean canMemorize() {
		return true;
	}

	protected static void setTargetPosition(SimpleMoveAgentController agent,
			Vector2D destination) {
		// we generate a simple and fast plan
		BasicLocation dest = agent.getClosestBasicLocation(destination);
		List<AStarMoveAction> simplePlan = agent.generateSimpleFastPlan(dest);

		// we can reach destination
		if (simplePlan != null) {
			simplePlan = SimpleMoveAgentController.cleanPlan(simplePlan);
			// we give this plan as temporary before we calculate the real plan
			agent.setPlan(simplePlan);
			CompleteLocation realDest = agent
					.getClosestCompleteLocation(destination);
			List<AStarMoveAction> complexPlan = agent
					.generateRealPlan(realDest);
			if (complexPlan != null) {
				// we can reach destination no one is blocking us
				complexPlan = SimpleMoveAgentController.cleanPlan(complexPlan);
				agent.setPlan(complexPlan);
			} else {
				// we cannot reach destination
				System.out.println(" we cannot reach destination");
			}
		}
	}

	public static Boolean canReachTargetPosition(
			SimpleMoveAgentController agent, Vector2D destination) {
		List<AStarMoveAction> finalPlan = null;

		BasicLocation dest = agent.getClosestBasicLocation(destination);
		finalPlan = agent.generateSimpleFastPlan(dest);

		return finalPlan != null;
	}

	public static List<AStarMoveAction> generatePlanToTargetPosition(
			SimpleMoveAgentController agent, Vector2D destination) {

		List<AStarMoveAction> finalPlan = null;

		// we generate a simple and fast plan
		BasicLocation dest = agent.getClosestBasicLocation(destination);
		List<AStarMoveAction> simplePlan = agent.generateSimpleFastPlan(dest);
		List<AStarMoveAction> complexPlan = null;

		finalPlan = SimpleMoveAgentController.cleanPlan(simplePlan);
		CompleteLocation realDest = agent
				.getClosestCompleteLocation(destination);
		complexPlan = agent.generateRealPlan(realDest);
		if (complexPlan != null) {
			// we can reach destination no one is blocking us
			finalPlan = SimpleMoveAgentController.cleanPlan(complexPlan);
		}

		return finalPlan;
	}
}
